<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	
    public function index(){
    	$url=$_GET['blog'];
    	$art = M('article');
    	$guid = '';
		$whereGuid = Array();
		if($url!=null){
			$user = M('user')->where(array('username'=>$url))->find();
			$guid=$user['guid'];
			$whereGuid['guid'] = $guid; 
		}
		//echo $whereGuid;		
		header ( 'Content-type: text/html; charset=utf-8' );
		$sql = M ( 'article' );
		
		$count = $art->where ( $whereGuid )->count (); // 查询满足要求的总记录数
		$Page = new \Think\Page ( $count, 5 ); // 实例化分页类 传入总记录数和每页显示的记录数
		
		$cateArr = M ( 'cate' )->where ( $whereGuid )->select ();
		// 把分类转换为二维数组 array('cid'=>'cate_name')
		$cate = array ();
		for($i = 0; $i < count ( $cateArr ); $i ++) {
			$cate [$cateArr [$i] ['cid']] = $cateArr [$i] ['cate_name'];
		}
		
		$data = $art->where ( $whereGuid )->order ( 'sort' )->limit ( $Page->firstRow . ',' . $Page->listRows )->select ();
		
		// 把$data中的分类cid转化为对应分类中的cate_name
		for($i = 0; $i < count ( $data ); $i ++) {
			$data [$i] ['cid'] = $cate [$data [$i] ['cid']];
		}
		
		// $this->assign('cid',$cid);
		$Page->setConfig ( 'header', '<li><a>共 %TOTAL_ROW% 条记录</a></li>' );
		$Page->setConfig ( 'prev', '上一页' );
		$Page->setConfig ( 'next', '下一页' );
		$Page->setConfig ( 'first', '第一页' );
		$Page->setConfig ( 'theme', "%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%" );
		
		$show = $Page->show (); // 分页显示输出
		$this->assign ( 'page', $show ); // 赋值分页输出
		$this->assign ( 'art', $data ); // 赋值分页输出
		// $this->art = $data;
		$this->display ();
    }
    
    public function artList(){
    	$art = M('article');
    	//echo $art->_sql () . 'zhege';
    	$this->art = $art->select();
    	
    	$this->display();
    }
}